PowerShell in Practice by Thomas Blake
Author: Thomas Blake , Date: September 12, 2023
, Views: 330
# Using ForEach-Object Cmdlet with Pipeline
$names | ForEach-Object {
Write-Output "Greetings, $_!"
}
6.3.4 Using Continue and Break
These statements control the flow within loops:
Continue: Skips the current iteration and moves to the next one.
Break: Exits the loop entirely.
Example:
for ($i = 1; $i -le 10; $i++) {
if ($i -eq 4) { continue } # Skip when $i is 4
if ($i -eq 8) { break } # Exit loop when $i is 8
Write-Output $i
}
6.3.5 Nested Loops
Loops within loops offer greater granularity and control but also require careful management to avoid complexity.
Example:
for ($i = 1; $i -le 3; $i++) {
for ($j = 1; $j -le 3; $j++) {
Write-Output "Outer loop iteration: $i, Inner loop iteration: $j"
}
}
Copyright Disclaimer:
This site does not store any files on its server. We only index and link
to content provided by other sites. Please contact the content providers
to delete copyright contents if any and email us, we'll remove relevant
links or contents immediately.